package com.iflytek.dao.impl;/**
 * @Author qing
 * @Date 2022/3/15 15:57
 */

import com.iflytek.bean.Emp;
import com.iflytek.dao.EmpDao;
import com.iflytek.util.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

/**
 * @author qingyunchen
 * @Description
 * @Date: 2022/3/15 15:57
 * @Version 1.0
 * //员工数据库操作实现类
 */
public class EmpDaoImpl implements EmpDao {
    @Override
    //获取员工信息
    public List<Emp> getAllEmp() {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        List<Emp> empList = null;
        try {
            empList = queryRunner.query("select * from  emp", new BeanListHandler<Emp>(Emp.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        System.out.println("所有员工信息如下：");
        return empList;
    }

    @Override
    //新增员工信息
    public void addEmp(Emp emp) {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入员工编号:");
        String empno = sc.next();
        System.out.print("请输入员工姓名:");
        String ename  = sc.next();
        System.out.print("请输入员工工作类型:");
        String job = sc.next();
        System.out.print("请输入员工薪资:");
        String sal = sc.next();
        System.out.print("请输入员工入职日期:");
        String hiredate = sc.next();
        System.out.print("请输入员工奖金:");
        String comm = sc.next();
        System.out.print("请输入员工部门编号:");
        String deptno = sc.next();
        try {
            queryRunner.update("insert into emp value (?,?,?,?,?,?,?)",empno,ename,job,sal,hiredate,comm,deptno);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    //根据工号修改员工信息
    public void upDate(int empno) {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());//update emp set ename=?,job=?,sal=?,hiredate=?,comm=?,deptno=? where empno=?
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入员工编号:");
        String empno1 = sc.next();
        System.out.print("请输入修改后员工姓名:");
        String ename  = sc.next();
        System.out.print("请输入修改后员工工作类型:");
        String job = sc.next();
        System.out.print("请输入修改后员工薪资:");
        String sal = sc.next();
        System.out.print("请输入修改后员工入职日期:");
        String hiredate = sc.next();
        System.out.print("请输入修改后员工奖金:");
        String comm = sc.next();
        System.out.print("请输入修改后员工部门编号:");
        String deptno = sc.next();
        try {
            queryRunner.update("update emp set ename=?,job=?,sal=?,hiredate=?,comm=?,deptno=? where empno=?",ename,job,sal,hiredate,comm,deptno,empno1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    //根据员工编号删除员工
    public void delEmpById(int empno) {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        Scanner sc=new Scanner(System.in);
        System.out.print("请输入删除员工编号:");
        String empno1 = sc.next();
        try {
            queryRunner.update("delete from emp where empno=?",empno1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    //根据姓名模糊查询
    public List<Emp> selectLikeName(int empno) {
        QueryRunner queryRunner = new QueryRunner(DbUtils.getDataSource());
        List<Emp> query = null;
        try {
            Scanner sc=new Scanner(System.in);
            System.out.print("请输入:");
            String ename = sc.next();
            query = queryRunner.query("select * from emp where ename like '%"+ename+"%';", new BeanListHandler<Emp>(Emp.class));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return query;
    }

    @Override
    public void exit() {
        System.out.println("退出成功！");
    }
}